package com.jindy.module.stock.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.jindy.module.product.domain.ProductModel;
import com.jindy.module.product.mapper.ProductModelMapper;
import com.jindy.module.stock.domain.Stock;
import com.jindy.module.stock.dto.StockDto;
import com.jindy.module.stock.dto.StockVo;
import com.jindy.module.stock.mapper.StockMapper;
import com.jindy.module.stock.service.IStockService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 库存Service业务层处理
 *
 * @author ruoyi
 * @date 2022-10-08
 */
@Service
public class StockServiceImpl implements IStockService
{
    @Resource
    private StockMapper stockMapper;

    @Resource
    private ProductModelMapper productModelMapper;

    /**
     * 查询库存
     *
     * @param id 库存主键
     * @return 库存
     */
    @Override
    public Stock selectStockById(Long id)
    {
        return stockMapper.selectStockById(id);
    }

    /**
     * 查询库存列表
     *
     * @param stock 库存
     * @return 库存
     */
    @Override
    public List<Stock> selectStockList(Stock stock)
    {
        return stockMapper.selectStockList(stock);
    }

    /**
     * 查询库存列表及每种型号价格（非模糊查询）
     *
     * @param stock 库存
     * @return 库存
     */
    @Override
    public List<StockVo> selectStockList3(Stock stock)
    {
        List<StockVo> stockVoList = new ArrayList<>();

        List<Stock> stockList = stockMapper.selectStockList3(stock);
        if(!CollectionUtils.isEmpty(stockList)) {
            stockList.forEach(stock1 -> {
                ProductModel productModel = new ProductModel();
                productModel.setModel(stock1.getModel());
                List<ProductModel> productModelList = productModelMapper.selectProductModelList(productModel);
                ProductModel productModel1 = productModelList.get(0);

                StockVo stockVo = new StockVo();
                stockVo.setId(stock1.getId());
                stockVo.setModel(stock1.getModel());
                stockVo.setPrice(productModel1.getPrice());
                stockVo.setProductId(stock1.getProductId());
                stockVo.setProductName(stock1.getProductName());
                stockVo.setProductNo(stock1.getProductNo());
                stockVo.setQuantity(stock1.getQuantity());
                stockVo.setWarehouseId(stock1.getWarehouseId());
                stockVo.setWarehouseName(stock1.getWarehouseName());
                stockVo.setAvailableQuantity(stock1.getAvailableQuantity());
                stockVo.setStartQuantity(stock1.getStartQuantity());

//                BeanUtils.copyProperties(stockVo,stock1);
                stockVoList.add(stockVo);
            });
        }
        return stockVoList;
    }


    /**
     * 查询库存列表（根据型号排序）
     *
     * @param stock 库存
     * @return 库存
     */
    @Override
    public List<StockDto> selectStockDtoList(Stock stock)
    {
        List<StockDto> list = stockMapper.selectStockDtoList(stock);
        if(!CollectionUtils.isEmpty(list)){
            list.forEach(stockDto -> {
                //查询各个仓库库存
                List<StockDto> stockDtoList = stockMapper.selectStockByProduct(stockDto);
                stockDto.setStockQuantityList(stockDtoList);
            });
        }
        return list;
    }

    /**
     * 查询库存列表（根据仓库名排序）
     *
     * @param stock 库存
     * @return 库存
     */
    @Override
    public List<StockDto> selectStockDtoList2(Stock stock)
    {
        List<StockDto> list = stockMapper.selectStockDtoList2(stock);
        if(!CollectionUtils.isEmpty(list)){
            list.forEach(stockDto -> {
                //查询各个仓库库存
                List<StockDto> stockDtoList = stockMapper.selectStockByProduct(stockDto);
                stockDto.setStockQuantityList(stockDtoList);
            });
        }
        return list;
    }

    /**
     * 新增库存
     *
     * @param stock 库存
     * @return 结果
     */
    @Override
    public int insertStock(Stock stock)
    {
        return stockMapper.insertStock(stock);
    }

    /**
     * 修改库存
     *
     * @param stock 库存
     * @return 结果
     */
    @Override
    public int updateStock(Stock stock)
    {
        return stockMapper.updateStock(stock);
    }

    /**
     * 批量删除库存
     *
     * @param ids 需要删除的库存主键
     * @return 结果
     */
    @Override
    public int deleteStockByIds(Long[] ids)
    {
        return stockMapper.deleteStockByIds(ids);
    }

    /**
     * 删除库存信息
     *
     * @param id 库存主键
     * @return 结果
     */
    @Override
    public int deleteStockById(Long id)
    {
        return stockMapper.deleteStockById(id);
    }

    @Override
    public Stock getStock(StockDto stock) {
        List<StockDto> stockDtoList = stockMapper.selectStockByProduct(stock);
        if(!CollectionUtils.isEmpty(stockDtoList)){
            return stockDtoList.get(0);
        }
        return null;
    }
}
